package com.chenqi.myblog.controller.admin;

import com.chenqi.myblog.model.Tag;
import com.chenqi.myblog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @description:
 * @program: myblog
 * @author: ChenQi
 * @create: 2021-02-2021/2/2-09-50
 */
@Controller
@RequestMapping("/admin")
public class ATagController {

    @Autowired
    private TagService tagService;

    /**
     * 查找所有标签(不分类)
     * @param modelAndView
     * @return
     */

    @GetMapping("/tag_list")
    public ModelAndView getTags(ModelAndView modelAndView) {
        List<Tag> tags = tagService.findAllTag();
        modelAndView.addObject("tags", tags);
        modelAndView.setViewName("admin/tags");
        return modelAndView;
    }

    /**
     * 保存标签的方法
     * @param name
     * @param modelAndView
     * @return
     */

    @PostMapping("/saveTag")
    public ModelAndView saveTag(@RequestParam("name") String name,ModelAndView modelAndView) {
        Tag tag = new Tag();
        tag.setName(name);
        tagService.saveTag(tag);
        modelAndView.setViewName("redirect:/admin/tag_list");
        return modelAndView;
    }

    /**
     * 删除标签
     * @param id
     * @param modelAndView
     * @return
     */

    @GetMapping("/removeTag")
    public ModelAndView remove(@RequestParam("id") Integer id,ModelAndView modelAndView) {
        tagService.removeTag(id);
        modelAndView.setViewName("redirect:/admin/tag_list");
        return modelAndView;
    }
}
